/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.handlers.commands;

import com.hercules.android.receiver.AndroidReceiver;
import com.hercules.android.receiver.LogUtil;
import com.hercules.android.receiver.handlers.commands.CommandHandlersUtil.ValidationException;
import com.hercules.android.receiver.model.commands.XRECommand;
import com.hercules.android.receiver.model.commands.XREConnectRejectedCommand;

/**
 * The handler used to process XREConnectRejectedCommand. It simply delegates to a configured auxiliary handler. It
 * extends XRECommandHandler for convenience.
 * <p>
 * <b>Sample Usage:</b> Please look at sample usage of {@link XREAggregateCommandHandler}.
 * <p>
 * <b>Thread Safety:</b> This class is not thread safe since it has mutable fields. But if they are not changed after
 * first configured, then this class is used in a thread safe manner.
 *
 * @author liuliquan, 7realm
 * @version 1.0
 */
public class XREConnectRejectedCommandHandler extends XRECommandHandler {
    /**
     * The auxiliary handler to be delegated for actual processing.
     * <p>
     * Initial value is null. Can be any value. If it is null then no processing will be performed. It has getter and
     * setter.
     */
    private AuxiliaryXRECommandHandler connectRejectedCommandAuxiliaryHandler;

    /**
     * Default empty constructor.
     */
    public XREConnectRejectedCommandHandler() {
        // empty
    }

    /**
     * Handle given CONNECT_REJECTED command message.
     *
     * @param command the XRE CONNECT_REJECTED command to be handled
     * @param receiver the receiver for the XRE application
     * @throws IllegalArgumentException if given command or receiver is null
     * @throws XRECommandHandlerException if any error occurs while handling the command
     */
    @Override
    protected void processCommandMessage(XRECommand command, AndroidReceiver receiver)
        throws XRECommandHandlerException {
        String methodName = "processCommandMessage";
        LogUtil.enter(getLogger(), this, methodName, command, receiver);

        try {
            // check arguments, receiver fields are not checked, because the are not used
            CommandHandlersUtil.checkCommand(command, XREConnectRejectedCommand.class);
            CommandHandlersUtil.checkNotNull(receiver, "receiver");

            // process command if auxiliary handler is present
            if (connectRejectedCommandAuxiliaryHandler != null) {
                connectRejectedCommandAuxiliaryHandler.processCommandMessage(command, receiver);
            }
            LogUtil.exit(getLogger(), this, methodName);
        } catch (IllegalArgumentException e) {
            throw LogUtil.error(getLogger(), this, methodName, e);
        } catch (XRECommandHandlerException e) {
            throw LogUtil.error(getLogger(), this, methodName, e);
        } catch (ValidationException e) {
            throw LogUtil.error(getLogger(), this, methodName, new XRECommandHandlerException(e.getMessage(), e));
        }
    }

    /**
     * Gets the auxiliary handler to be delegated for actual processing.
     *
     * @return the auxiliary handler to be delegated for actual processing
     */
    public AuxiliaryXRECommandHandler getConnectRejectedCommandAuxiliaryHandler() {
        return connectRejectedCommandAuxiliaryHandler;
    }

    /**
     * Sets the auxiliary handler to be delegated for actual processing.
     *
     * @param connectRejectedCommandAuxiliaryHandler the auxiliary handler to be delegated for actual processing
     */
    public void setConnectRejectedCommandAuxiliaryHandler(
        AuxiliaryXRECommandHandler connectRejectedCommandAuxiliaryHandler) {
        this.connectRejectedCommandAuxiliaryHandler = connectRejectedCommandAuxiliaryHandler;
    }
}